package com.zxc.oauth.config

import com.zxc.oauth.model.CurrentUser
import com.zxc.oauth.properties.OauthUserProperties
import com.zxc.oauth.service.TokenService
import com.zxc.oauth.service.impl.UserTokenService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@EnableConfigurationProperties(value = [OauthUserProperties::class])
@ConditionalOnClass(OauthUserConfig::class)
@ConditionalOnProperty(
    prefix = "oauth.user",
    value = ["enabled"],
    matchIfMissing = true
)
class OauthUserConfig {

    @Autowired
    private lateinit var oauthUserProperties: OauthUserProperties

    @Bean
    fun userTokenService(): TokenService<CurrentUser>?{
        if(oauthUserProperties.accessTokenSecret == null
            || oauthUserProperties.refreshTokenSecret == null
            || oauthUserProperties.accessTokenExpiresIn == null
            || oauthUserProperties.refreshTokenExpiresIn == null){
            return null
        }
        return UserTokenService(oauthUserProperties)
    }
}